﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;

namespace PropGridInfo
{
	public class EnumConverter : TypeConverter
	{
		public EnumConverter()
		{
			this.dic = new Dictionary<object, string>();
		}

		private void LoadDic(ITypeDescriptorContext context)
		{
			this.dic = this.GetEnumValueDesDic(context.PropertyDescriptor.PropertyType);
		}

		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			bool flag = sourceType == typeof(string);
			return flag || base.CanConvertFrom(context, sourceType);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			bool flag = value is string;
			if (flag)
			{
				bool isEnum = context.PropertyDescriptor.PropertyType.IsEnum;
				if (isEnum)
				{
					bool flag2 = this.dic.Count <= 0;
					if (flag2)
					{
						this.LoadDic(context);
					}
					bool flag3 = this.dic.Values.Contains(value.ToString());
					if (flag3)
					{
						foreach (object obj in this.dic.Keys)
						{
							bool flag4 = this.dic[obj] == value.ToString();
							if (flag4)
							{
								return obj;
							}
						}
					}
				}
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return true;
		}

		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
		{
			return true;
		}

		public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
		{
			bool flag = this.dic == null || this.dic.Count <= 0;
			if (flag)
			{
				this.LoadDic(context);
			}
			return new TypeConverter.StandardValuesCollection(this.dic.Keys);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			bool flag = this.dic.Count <= 0;
			if (flag)
			{
				this.LoadDic(context);
			}
			foreach (object obj in this.dic.Keys)
			{
				bool flag2 = obj.ToString() == value.ToString() || this.dic[obj] == value.ToString();
				if (flag2)
				{
					return this.dic[obj].ToString();
				}
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

		public Dictionary<object, string> GetEnumValueDesDic(Type enumType)
		{
			Dictionary<object, string> dictionary = new Dictionary<object, string>();
			FieldInfo[] fields = enumType.GetFields();
			foreach (FieldInfo fieldInfo in fields)
			{
				bool isEnum = fieldInfo.FieldType.IsEnum;
				if (isEnum)
				{
					object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
					bool flag = customAttributes.Length != 0;
					if (flag)
					{
						dictionary.Add(Enum.Parse(enumType, fieldInfo.Name), ((DescriptionAttribute)customAttributes[0]).Description);
					}
				}
			}
			return dictionary;
		}

		private Dictionary<object, string> dic;
	}
}
